package aszm.chahaospider.parser;

import java.util.List;

import org.apache.log4j.Logger;

import aszm.chahaospider.constants.Constants;
import aszm.chahaospider.parser.chahao.CityParser;
import aszm.chahaospider.parser.chahao.PhoneHomeParser;
import aszm.chahaospider.parser.chahao.ProviceParser;
import aszm.chahaospider.parser.chahao.bean.Tmp_City;
import aszm.chahaospider.parser.dytt.DyzzParser;
import aszm.chahaospider.parser.sgls.SglsParser;
import aszm.chahaospider.parser.shouji.ShoujiParser;
import chaohao.mybatis.model.ISPDto;
import chaohao.mybatis.model.ProviceDto;

public class Fetch {
	private static final Logger Log = Logger.getLogger(Fetch.class.getName());

	public static void initializefetchProvice() {
		try {
			ProviceParser.getProviceFromWeb(Constants.CHAHAO_URL + Constants.CHAHAO_ALL_URL, false);
		} catch (Exception e) {
			Log.error(e);
		}
	}

	public static void initializefetchCity() {
		try {
			List<ProviceDto> provices = ProviceParser.getProviceFromDB();
			for (ProviceDto tb_Provice : provices) {
				Log.info("开始获取" + tb_Provice.getName());
				CityParser.getCitysFromWeb(tb_Provice, false);
			}
		} catch (Exception e) {
			Log.error(e);
		}
	}

	public static void initializefetchNumber() {
		List<ISPDto> isps = ShoujiParser.getAllShoujiFromDB();
		List<Tmp_City> citys = CityParser.getTmpCitysFromDB();
		int count = 0;
		for (Tmp_City city : citys) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					Log.info("开始抓取--" + city.getId() + "--" + city.getCity());
					PhoneHomeParser.getNumberFromWeb(city, isps, true);
				}
			}).start();
			if (count % 10 == 0) {
				try {
					Log.info("30s后再次抓取……");
					Thread.sleep(30 * 1000);
				} catch (InterruptedException e) {
				}
			}
			count++;
		}
	}

	public static void initializefetchShouji() {
		ShoujiParser.getShoujiFromWeb(Constants.SHOUJI_URL, false);
	}

	public static void initializefetchDyzz() {
		for (int i = 1; i < 1; i++) {
			DyzzParser.getDyzzFromWeb(String.format(Constants.ZXDY_URL, i), false);
			try {
				Thread.sleep(5 * 1000);
			} catch (InterruptedException e) {
				Log.error(e);
			}
		}
	}

	/**
	 * 开始解析三国乱世游戏活动
	 */
	public static void initializefetchSglsGameActivitys() {
		int totalPage = 2;
		for (int i = 1; i < totalPage; i++) {
			SglsParser.getSglsGameActivitysFromWeb(String.format(Constants.SGLS_URL, i), false);
			try {
				Thread.sleep(5 * 1000);
			} catch (InterruptedException e) {
				Log.error(e);
			}
		}
	}

}
